<?php
/**
 * @title 397. 整数替换 - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param Integer $n
     * @return Integer
     */
    function integerReplacement($n) {
        /**
         * @since 2020-08-12 终点
         */
        if($n == 1){
            return 0;
        } else {}

        /**
         * @since 2020-08-12 偶数
         */
        if($n%2 == 0){
            return 1+self::integerReplacement($n>>1);
        } else {
            /**
             * @since 2020-08-12 奇数
             */
            return 1+min(self::integerReplacement($n+1), self::integerReplacement($n-1));
        }
    }
}



/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.12_4.php
 */
$datas = [
    8,
    7,
    1,
    15,
    2,
    pow(2,31)-1,
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->integerReplacement($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
